package com.emapp.lccode.http

import com.emapp.httplogginginterceptorlibrary.DefaultHttpLoggingInterceptor
import com.emapp.httplogginginterceptorlibrary.LOG
import com.emapp.lccode.base.BaseConfig
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.network.BaseNetworkApi
import me.hgj.jetpackmvvm.network.interceptor.CacheInterceptor
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

/**
 * 描述　: 网络请求构建器，继承BasenetworkApi 并实现setHttpClientBuilder/setRetrofitBuilder方法，
 * 在这里可以添加拦截器，设置构造器可以对Builder做任意操作
 */


//双重校验锁式-单例 封装NetApiService 方便直接快速调用简单的接口
val apiService: ApiService by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    NetworkApi.INSTANCE.getApi(ApiService::class.java, ApiService.SERVER_URL)
}

class NetworkApi : BaseNetworkApi() {

    companion object {
        val INSTANCE: NetworkApi by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            NetworkApi()
        }
    }

    /**
     * 实现重写父类的setHttpClientBuilder方法，
     * 在这里可以添加拦截器，可以对 OkHttpClient.Builder 做任意操作
     */
    override fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder {
        var logInterceptor = DefaultHttpLoggingInterceptor()
        if (BaseConfig.isDebug) {
            logInterceptor =
                DefaultHttpLoggingInterceptor(DefaultHttpLoggingInterceptor.Level.REQUEST_RESPONSE)
            logInterceptor.setLog(LOG.ERROR)
        } else {
            logInterceptor =
                DefaultHttpLoggingInterceptor(DefaultHttpLoggingInterceptor.Level.NONE)
        }
        builder.apply {
            //设置缓存配置 缓存最大10M
            // cache(Cache(File(appContext.cacheDir, "cxk_cache"), 10 * 1024 * 1024))
            //添加Cookies自动持久化
            cookieJar(cookieJar)
            //示例：添加公共heads 注意要设置在日志拦截器之前，不然Log中会不显示head信息
            addInterceptor(MyHeadInterceptor())
            //添加缓存拦截器 可传入缓存天数，不传默认7天
//            addInterceptor(CacheInterceptor())
            addInterceptor(TokenOutInterceptor())
            // 日志拦截器
            addInterceptor(logInterceptor)
            //超时时间 连接、读、写
            connectTimeout(10, TimeUnit.SECONDS)
            readTimeout(10, TimeUnit.SECONDS)
            writeTimeout(10, TimeUnit.SECONDS)
            hostnameVerifier(SSLSocketClient.getHostnameVerifier())
        }
        return builder
    }

    /**
     * 实现重写父类的setRetrofitBuilder方法，
     * 在这里可以对Retrofit.Builder做任意操作，比如添加GSON解析器，protobuf等
     */
    override fun setRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder {

        return builder.apply {
            val adapterFactory2 = GsonBuilder().registerTypeAdapterFactory(MyTypeAdapterFactory2())
                .create() //.disableHtmlEscaping()
            addConverterFactory(GsonConverterFactory.create(adapterFactory2))//解决解析null报错的问题
          //  addConverterFactory(ScalarsConverterFactory.create())

            // addConverterFactory(CustomGsonConverterFactory.create())//解决同一个接口返回data格式不统一的问题
          //  addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            // addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
        }
    }

    val cookieJar: PersistentCookieJar by lazy {
        PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(appContext))
    }
}



